<?php

class Page extends Model {

	function Page()
    {
        parent::Model();
    }
    
    function get_page($id)
	{
		$this->db->select('pages.*, COUNT(comments.id) AS num_comments, comments.body AS comm_body, comments.uid AS comm_auth');
		$this->db->from('pages');
		$this->db->where('pages.id', $id);
		$this->db->join('comments', 'comments.pid = pages.id', 'left');
		//$this->db->groupby("pages.tid");
		//$this->db->orderby("pages.date", "desc"); 
		$query = $this->db->get();
		
		$page_data = $query->row();
		
		$translations = $this->get_translations($page_data->tid);
		
		$page_data->translations = $translations;
		
		return $page_data;
	}
    
	function get_all_pages($conditions = NULL)
	{
		$this->db->select('pages.*, COUNT(comments.id) AS num_comments');
		$this->db->from('pages');
		$this->db->where($conditions); 
		$this->db->join('comments', 'comments.pid = pages.id', 'left');
		$this->db->groupby("pages.id");
		$this->db->orderby("pages.date", "desc"); 
		$query = $this->db->get();
		
		return $query->result();
	}
	
	function get_translations($tid)
	{
		$this->db->select('id, language');
		$this->db->from('pages');
		$this->db->where('tid', $tid);
		$query = $this->db->get();
		
		return $query->result();
	}
	
	function save()
	{
		$data = array(
			'title' => $this->input->post('page_title'),
			'sid' => $this->input->post('page_section'),
			'cid' => $this->input->post('page_category'),
			'intro' => $this->input->post('page_intro'),
			'body' => $this->input->post('page_body'),
			'status' => $this->input->post('page_status'),
			'language' => $this->langs->current_lang
		);
		
		if ($this->db->insert('pages', $data))
		{
			return TRUE;
		}
		else 
		{
			return FALSE;
		}
	}
}
?>
